﻿/**
 * 初始化分页
 * param:Object {pageId,url,callback, [rows], [itemClick], [currPage]}
 * 参数说明:
 * pageId : 分页div的id
 * url : ajax请求数据的地址
   data: 查询的参数
 * callback : 请求数据完成的回调方法
 * rows :[可选，默认10]每页显示的条数
 * itemClick: click时调用的方法
 * ajaxStart: ajax请求之前调用
 * ajaxStop: ajax请求完成后调用
 * currPage：初始化到指定当前页
 * jump : 是否回到页面顶端（true：是，false：否）
 * 调用参考 :
 * _initPagination({pageId:'page1', url : 'xxx.do', jump : false, callback: callback});	 
 */
var _rows = 10;
var _tmpPagination;
function _initPagination(param){
	var rows = 10; //每页默认10条
	var currPage = 1;
	var pageId = param.pageId;
    var cssStyle = "green";
    var jump = true;
    var clickPage = false;
    var key = '';// 用于存取cookie分页
    var urlParams = new Object(); 
    if(param.jump != undefined){
    	jump = param.jump;
    }
	if(param.rows){
		rows = param.rows;
	}
	_rows = rows;
	if(param.currPage){
		currPage = param.currPage;
		_currPage = currPage;
	}
	if(param.cKey){
		try {
			var cPage = $.cookie(param.cKey);
			if (cPage == null) {
				cPage = 1;
			}
			cPage = parseInt(cPage);
			currPage = cPage;
			_currPage = cPage;
		} catch(e) {}
	}
    if (param.cssStyle) {
        cssStyle = param.cssStyle;
    }
    if (param.key) {
        key = param.key;
    }
	if (param.data) {// ajax 参数
		urlParams = param.data;
	}
	urlParams.rows = rows;
	
	var jsonMyPagination = jQuery("#" + pageId).myPagination({
		panel : {
			tipInfo_on : true,
			tipInfo : '&nbsp;&nbsp;跳{input}/{sumPage}页',
			tipInfo_css : {
				width : '35px',
				height : "20px",
				border : "1px solid #f0f0f0",
				padding : "0 0 0 5px",
				margin : "0 5px 0 5px"
			}
		},
        cssStyle: cssStyle,
		pageRows : rows,
		currPage : currPage,
		ajax : {
			on : true,
			type : "post",
			dataType : 'json',
			url : param.url,
			param : urlParams,
			pageCountId : "total",
			onClick : function(p) {
                clickPage = true;
				if(param.itemClick){
					param.itemClick(p);
				}
			},
			callback : function(data) {
				param.callback(data);
				var tmpPage = jsonMyPagination.getPage();
				if(param.cKey){
					try {// 防止没有导入cookie出错
						$.cookie(param.cKey, tmpPage, {path:'/'});
					} catch(e) {}
				}
				if(data.total > rows){
					jQuery("#"+pageId).show();
				} else {
					jQuery("#"+pageId).hide();
				}
				var tPages = Math.ceil(data.total/rows);
				if(jsonMyPagination.getPage() > tPages){
					jsonMyPagination.jumpPage(tPages);
				}
                if(jump && clickPage){
                    if(parent.scrollTo){
                        clickPage = false;
                    }
                } 
			},
            ajaxStart : function() {
                if (param.ajaxStart) {
                    param.ajaxStart();
                }
            },
            ajaxStop : function() {
                if (param.ajaxStop) {
                    param.ajaxStop();
                }
            }
		}
	});
	_tmpPagination = jsonMyPagination;
	return jsonMyPagination;
}
function _initPaginationOnly(param){
	var rows = 10; //每页默认10条
	var currPage = 1;
	var pageId = param.pageId;
    var cssStyle = "greenonly";
    var jump = true;
    var clickPage = false;
    var key = '';// 用于存取cookie分页
    var urlParams = new Object(); 
    if(param.jump != undefined){
    	jump = param.jump;
    }
	if(param.rows){
		rows = param.rows;
	}
	_rows = rows;
	if(param.currPage){
		currPage = param.currPage;
		_currPage = currPage;
	}
	if(param.cKey){
		try {
			var cPage = $.cookie(param.cKey);
			if (cPage == null) {
				cPage = 1;
			}
			cPage = parseInt(cPage);
			currPage = cPage;
			_currPage = cPage;
		} catch(e) {}
	}
    if (param.cssStyle) {
        cssStyle = param.cssStyle;
    }
    if (param.key) {
        key = param.key;
    }
	if (param.data) {// ajax 参数
		urlParams = param.data;
	}
	urlParams.rows = rows;
	
	var jsonMyPagination = jQuery("#" + pageId).myPagination({
		panel : {
			first_on : false,
			last_on : false,
			tipInfo_on : false,
			tipInfo : '&nbsp;&nbsp;跳{input}/{sumPage}页',
			tipInfo_css : {
				width : '35px',
				height : "20px",
				border : "1px solid #f0f0f0",
				padding : "0 0 0 5px",
				margin : "0 5px 0 5px"
			}
		},
        cssStyle: cssStyle,
		pageRows : rows,
		currPage : currPage,
		ajax : {
			on : true,
			type : "post",
			dataType : 'json',
			url : param.url,
			param : urlParams,
			pageCountId : "total",
			onClick : function(p) {
                clickPage = true;
				if(param.itemClick){
					param.itemClick(p);
				}
			},
			callback : function(data) {
				param.callback(data);
				var tmpPage = jsonMyPagination.getPage();
				if(param.cKey){
					try {// 防止没有导入cookie出错
						$.cookie(param.cKey, tmpPage, {path:'/'});
					} catch(e) {}
				}
				if(data.total > rows){
					jQuery("#"+pageId).show();
				} else {
					jQuery("#"+pageId).hide();
				}
				var tPages = Math.ceil(data.total/rows);
				if(jsonMyPagination.getPage() > tPages){
					jsonMyPagination.jumpPage(tPages);
				}
                if(jump && clickPage){
                    if(parent.scrollTo){
                        clickPage = false;
                    }
                } 
			},
            ajaxStart : function() {
                if (param.ajaxStart) {
                    param.ajaxStart();
                }
            },
            ajaxStop : function() {
                if (param.ajaxStop) {
                    param.ajaxStop();
                }
            }
		}
	});
	_tmpPagination = jsonMyPagination;
	return jsonMyPagination;
}
function _getRowNumber(i) {
	var currPge = _tmpPagination.getPage();
	if (currPge < 1) {
		currPge = 1;
	}
	return ((currPge - 1) * _rows) + i + 1;
}

function resetPage(cKey) {
	try {// 防止没有导入cookie出错
		$.cookie(cKey, null, {path:'/'});
	} catch(e) {}
}

